import json
from django.http import JsonResponse
from django.views import View

from locustApi.models import User
from .user_form import UserForm


class UserView(View):
    def post(self, request):
        """
        增加数据
        :param request:
        :return:
        """
        form = UserForm(None, json.loads(request.body.decode()))
        if form.is_valid():
            instance = form.save()
            ret = {
                'code': 0,
                'info': {
                    'userName': instance.userName,
                    'msg': '注册成功！',
                    'status': 'success'
                }
            }
            return JsonResponse(ret)
        else:
            ret = {
                'code': 1,
                'data': form.errors,
                'status': 'failed'
            }
            return JsonResponse(ret)

    def get(self, request):
        """
        查询(所有数据)
        :param request:
        :return:
        """
        query_set = User.objects.values()  # <QuerySet [{'name': 'Erics'}]>
        ret = {
            'code': 0,
            'data': list(query_set),  # 转换成列表,[{'name': 'Erics'}]
            'status': 'success'
        }
        return JsonResponse(ret)


class UserDetail(View):
    def get(self, request, pk):
        """
        查询单条数据
        :param request:
        :param pk:
        :return:
        """
        res = User.objects.filter(pk=pk).values()
        print(res)
        if res:
            ret = {
                'code': 0,
                'data': list(res),
                'status': 'success'
                # 'data': {
                #     'userName': res.userName,
                #     'phone': res.phone,
                #     'age': res.age,
                #     'registerTime': res.register_date
                # }
            }
            return JsonResponse(ret)
        else:
            ret = {
                'code': 1,
                'data': {
                },
                'msg': "查询用户不存在！",
                'status': 'success'
            }
            return JsonResponse(ret)

    def put(self, request, pk):
        """
        修改数据。restful规定更新数据，用put或者patch，put用得多一些
        :param pk:
        :param request:
        :return:
        """
        instence = User.objects.filter(pk=pk).first()
        if not instence:
            ret = {
                'code': 1,
                'data': {
                    'name': '数据不存在'
                },
                'status': 'success'
            }
            return JsonResponse(ret)
        # 借助form来修改(form中有新数据),降低了耦合度
        form = UserForm(instence, json.loads(request.body.decode()))
        if form.is_valid():
            instence = form.save()  # instence不更新也是可以的，还是查找到的和要修改的id是一样的
            ret = {
                'status': 0,
                'data': instence.pk
            }
            return JsonResponse(ret)
        else:
            ret = {
                'status': 1,
                'data': form.errors
            }
            return JsonResponse(ret)

    def delete(self, request, pk):
        """
        删除数据(删除单条数据)
        :param request:
        :param pk:
        :return:
        """
        User.objects.filter(pk=pk).delete()
        ret = {
            'status': 0,
            'data': []
        }
        return JsonResponse(ret)
